#!/usr/bin/env bash

PROJECT_NAME=suzuki
DUBBO_NAME=dubbo-${PROJECT_NAME}
PROJECT_CORE=${PROJECT_NAME}-core
docker-compose up -d
mvn clean package -Dmaven.test.skip -U
if [ $? -eq 0 ]; then
echo "sucess"
CONTAINER_ID=`docker ps -aqf name=${DUBBO_NAME}`
IPADDR=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}' | tr -d 'addr:'`
if [ ! $CONTAINER_ID ]; then
docker build -t $DUBBO_NAME:latest ${PROJECT_CORE}
docker run --rm --name=${DUBBO_NAME} -p 20890:20880 -p 8787:8797 -e EXPORT_PORT=20890 -e EXPORT_HOST=${IPADDR} -e ZK_HOST=${IPADDR} $DUBBO_NAME
else
docker stop $DUBBO_NAME
docker rm $DUBBO_NAME
docker build -t $DUBBO_NAME:latest ${PROJECT_CORE}
docker run --rm --name=${DUBBO_NAME} -p 20890:20880 -p 8787:8797 -e EXPORT_PORT=20890 -e EXPORT_HOST=${IPADDR} -e ZK_HOST=${IPADDR} $DUBBO_NAME
fi
else
echo "------------------------------"
echo "------------------------------"
echo "maven error"
echo "------------------------------"
echo "------------------------------"
fi